//
//  HomeCell4.m
//  Tunnel
//
//  Created by wbb on 2025/6/3.
//

#import "HomeCell4.h"
#import "YXJCVC.h"
#import "YXJCDetailVC.h"

@implementation HomeCell4

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (void)cellWithData:(id)data {
    [super cellWithData:data];
    
    self.lxLab.text = [self formatNumber:[data[@"offLineNum"]?:@"" floatValue]];
    self.onlineLab.text = [self formatNumber:[data[@"onLineNum"]?:@"" floatValue]];
    self.zxlLab.text = [self formatNumber:[data[@"onLineRatio"]?:@"" floatValue]];

    
    NSArray *offLineDeviceList = [NSArray arrayWithArray:data[@"offLineDeviceList"]];
    [self dataWithArr:offLineDeviceList];
    
    [self.stack1 addTarget:self action:@selector(onRowClick1)];
    [self.stack2 addTarget:self action:@selector(onRowClick2)];
    [self.stack3 addTarget:self action:@selector(onRowClick3)];

    [self.item1 addTarget:self action:@selector(onItemClick1)];
    [self.item2 addTarget:self action:@selector(onItemClick2)];
    [self.item3 addTarget:self action:@selector(onItemClick3)];
}

- (NSString *)formatNumber:(CGFloat)number {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle; // 十进制格式
    formatter.maximumFractionDigits = 1; // 最多保留 1 位小数
    formatter.minimumFractionDigits = 0; // 最少保留 0 位小数（整数时不显示）
    formatter.roundingMode = NSNumberFormatterRoundHalfUp; // 四舍五入（如 10.25 → 10.3）
    return [formatter stringFromNumber:@(number)];
}


- (void)dataWithArr:(NSArray *)alarmDeviceList  {
    _alarmDeviceList = alarmDeviceList;
    self.stack1.hidden = YES;
    self.stack2.hidden = YES;
    self.stack3.hidden = YES;
    self.tipLab.hidden = YES;
    
    if(alarmDeviceList.count == 1){
        NSDictionary *alarmDevice1 =alarmDeviceList[0];
        self.title1.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice1[@"direction"],alarmDevice1[@"devName"],alarmDevice1[@"pileNo"],alarmDevice1[@"id"]];
        self.detailLab1.text = [NSString stringWithFormat:@"%@",alarmDevice1[@"outCause"]];
        self.stack1.hidden = NO;

    }else if(alarmDeviceList.count == 2){
        NSDictionary *alarmDevice1 =alarmDeviceList[0];
        self.title1.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice1[@"direction"],alarmDevice1[@"devName"],alarmDevice1[@"pileNo"],alarmDevice1[@"id"]];
        self.detailLab1.text = [NSString stringWithFormat:@"%@",alarmDevice1[@"outCause"]];
        
        NSDictionary *alarmDevice2 =alarmDeviceList[1];
        self.title2.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice2[@"direction"],alarmDevice2[@"devName"],alarmDevice2[@"pileNo"],alarmDevice2[@"id"]];
        self.detailLab2.text = [NSString stringWithFormat:@"%@",alarmDevice2[@"outCause"]];
        self.stack1.hidden = NO;
        self.stack2.hidden = NO;
    }else if(alarmDeviceList.count >= 3){
        NSDictionary *alarmDevice1 =alarmDeviceList[0];
        self.title1.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice1[@"direction"],alarmDevice1[@"devName"],alarmDevice1[@"pileNo"],alarmDevice1[@"id"]];        self.detailLab1.text = [NSString stringWithFormat:@"%@",alarmDevice1[@"outCause"]];
        
        NSDictionary *alarmDevice2 =alarmDeviceList[1];
        self.title2.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice2[@"direction"],alarmDevice2[@"devName"],alarmDevice2[@"pileNo"],alarmDevice2[@"id"]];
        self.detailLab2.text = [NSString stringWithFormat:@"%@",alarmDevice2[@"outCause"]];
        
        NSDictionary *alarmDevice3 =alarmDeviceList[2];
        self.title3.text = [NSString stringWithFormat:@"%@・%@%@・%@",alarmDevice3[@"direction"],alarmDevice3[@"devName"],alarmDevice3[@"pileNo"],alarmDevice3[@"id"]];
        self.detailLab3.text = [NSString stringWithFormat:@"%@",alarmDevice3[@"outCause"]];
        
        self.stack1.hidden = NO;
        self.stack2.hidden = NO;
        self.stack3.hidden = NO;
    }else {
        self.tipLab.hidden = NO;
    }
}
- (IBAction)more:(UIButton *)sender {
    
    YXJCVC *vc = [[YXJCVC alloc] init];
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}

- (void)onRowClick1 {
    NSDictionary *alarmDevice1 =self.alarmDeviceList[0];
    YXJCDetailVC *vc = [[YXJCDetailVC alloc] init];
    vc.ID = alarmDevice1[@"id"];
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}
- (void)onRowClick2 {
    NSDictionary *alarmDevice1 =self.alarmDeviceList[1];
    YXJCDetailVC *vc = [[YXJCDetailVC alloc] init];
    vc.ID = alarmDevice1[@"id"];
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}
- (void)onRowClick3 {
    NSDictionary *alarmDevice1 =self.alarmDeviceList[2];
    YXJCDetailVC *vc = [[YXJCDetailVC alloc] init];
    vc.ID = alarmDevice1[@"id"];
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}

- (void)onItemClick1 {
    YXJCVC *vc = [[YXJCVC alloc] init];
    vc.onOffStatus = @"0";
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}
- (void)onItemClick2 {
    YXJCVC *vc = [[YXJCVC alloc] init];
    vc.onOffStatus = @"1";
    [[WDGlobal getCurrentViewController].navigationController pushViewController:vc animated:NO];
}
- (void)onItemClick3 {
    
}
@end
